<template>
  <div class="juzhong">
    <div class="login">
      <h4>WELCOME</h4>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="user">
          <el-input
            type="type"
            v-model="ruleForm.user"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
       <div class="heng"> <p>注册账号</p><p>|</p><p>忘记密码</p></div>
      </el-form>
    </div>
  </div>
</template>
<script>
import instance from "../request/api.js";
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        user: "amdin",
        pass: "123456789",
      },
      rules: {
        user: [
          {
            trigger: "blur",
            required: true,
            message: "用户名",
            pattern: /^[a-zA-Z0-9]{5,10}$/,
          },
        ],
        pass: [
          {
            trigger: "blur",
            required: true,
            message: "密码",
            pattern: /^[a-zA-Z0-9]{6,10}$/,
          },
        ],
      },
    };
  },
  methods: {
    async submitForm() {
      let user = this.ruleForm.user;
      let pass = this.ruleForm.pass;
      let { data } = await instance.get("http://localhost:3000/users", {
        params: { user, pass },
      });
      console.log(data);
      let token = data[0].token;
      localStorage.setItem("token", token);
      if (data.length > 0) {
        this.$message({
          message: "登录成功",
          type: "success",
          onClose: () => {
            this.$router.push({ name: "Home" });
          },
        });
      }
    },
  },
};
</script>
<style lang="less" scoped>
.juzhong{
  height: 920px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../assets/bg.jpg");
  h4{
    color: #fff;
  }
  .heng{
    width: 150px;
    display: flex;
    justify-content: space-around;
  }
}
.login {
  background: hsla(0, 0%, 100%, 0.5);
  width: 300px;
  text-align: center;
  border-radius: 10px;
  padding: 30px;
}
.demo-ruleForm{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
/peep/.el-form-item__content {
    line-height: 40px;
    position: relative;
    font-size: 14px;
     margin-left: 0px;
} 
</style>
